﻿@inject IHttpService _httpService
@inject IUserService _userService
@inject IPopupService PopupService

@inject NavigationManager NavigationManager
@inject IDataCacheService _dataCacheService


<CnGalWebSite.Components.Buttons.MasaButton Fab Icon="@(IsThumbsUp?"mdi-thumb-up ":"mdi-thumb-up-outline")" Tooltip="@(IsThumbsUp?"已点赞":"点赞")" Class="@Class" OnClick="OnClickStar" />


@code {
    [Parameter]
    public long Id { get; set; }

    [Parameter]
    public bool IsThumbsUp { get; set; }
    [Parameter]
    public string Class { get; set; }

    bool isReady = false;

    [CascadingParameter]
    private Task<AuthenticationState> authenticationStateTask { get; set; }
    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        var user = await authenticationStateTask;
        if (Id != 0 && isReady == false && user.User.Identity.IsAuthenticated)
        {
            try
            {
                //获取文章信息
                var AsyncModel = await _httpService.GetAsync<ArticleAsyncInforViewModel>("api/articles/GetArticleAsyncInfor/" + Id);
                IsThumbsUp = AsyncModel.IsThumbsUp;
                isReady = true;
                StateHasChanged();
            }
            catch (Exception ex)
            {
                await ErrorHandler.ProcessError(ex, "获取与用户相关的文章信息失败");
            }
        }

    }


    private async Task OnClickStar()
    {
        //判断用户是否登录
        var user = await authenticationStateTask;
        if (user.User.Identity.IsAuthenticated)
        {
            if (IsThumbsUp)
            {
                await UnThumbsUp();
            }
            else
            {
                await ThumbsUp();

            }
        }
        else
        {
            _userService.Login();
        }

    }

    private async Task UnThumbsUp()
    {
        try
        {

            var obj = await _httpService.PostAsync<ThumbsUpArticleModel, Result>("api/articles/ThumbsUp", new ThumbsUpArticleModel { Id = Id, IsThumbsUp = false });
            //判断结果
            if (obj.Successful == false)
            {
                await PopupService.ToastErrorAsync("取消点赞文章失败", obj.Error);
            }
            else
            {
                //await PopupService.ToastSuccessAsync("取消点赞文章成功", "取消点赞文章成功");
                IsThumbsUp = false;
                StateHasChanged();
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "取消点赞文章失败");
        }
    }
    private async Task ThumbsUp()
    {
        try
        {
            var obj = await _httpService.PostAsync<ThumbsUpArticleModel, Result>("api/articles/ThumbsUp", new ThumbsUpArticleModel { Id = Id, IsThumbsUp = true });
            //判断结果
            if (obj.Successful == false)
            {
                await PopupService.ToastErrorAsync("点赞文章失败", obj.Error);
            }
            else
            {
                //await PopupService.ToastSuccessAsync("点赞文章成功", "点赞文章成功");
                IsThumbsUp = true;
                StateHasChanged();
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "点赞文章失败");
        }
    }

}
